package make.free.app.mcaro.game;

import java.util.ArrayList;
import java.util.List;

import make.free.app.mcaro.activity.Game;
import make.free.app.mcaro.ai.AI;
import make.free.app.mcaro.util.Constan;
import android.graphics.Point;

public class EventTitle {
	public static boolean onclickItem = true;
	public static int count = 0;
	public static Title item1 = null;
	public static boolean Win = false;
	public static int player_change;
	public static boolean isEqual = false;
	public static int type = 0;
	public static int PlayerWin;
	public static Point point = new Point();

	// public static List<Title> list_tile = new ArrayList<Title>();
	public static List<Title> list_o = new ArrayList<Title>();
	public static List<Title> list_x = new ArrayList<Title>();

	public static void click(Title item) {
		item1 = item;
		checkRunPlayer();
	}

	public static void reset() {
		item1 = null;
		onclickItem = true;

	}

	public static void checkRunPlayer() {
		onclickItem = false;
		if (!AI.CheckEntry()) {
			EventTitle.isEqual = true;
		}
		if (player_change == 1) {
			if (checkStep(item1.getX(), item1.getY())) {
				if (Game.isX) {
					Game.game.crateX(item1.getPositionX(),
							item1.getPositionY(), item1.getX(), item1.getY());
					MT.list_item[item1.getX()][item1.getY()] = 0;
					if (!Game.isGameAI) {
						String masage = "4-" + item1.getX() + "-"
								+ item1.getY();
						Game.game.sendMessage(masage);
					}
					player_change = 2;

				} else {
					Game.game.crateO(item1.getPositionX(),
							item1.getPositionY(), item1.getX(), item1.getY());
					MT.list_item[item1.getX()][item1.getY()] = 1;
					if (!Game.isGameAI) {
						String masage = "4-" + item1.getX() + "-"
								+ item1.getY();
						Game.game.sendMessage(masage);
					}
					player_change = 2;

				}

				PlayerWin = CheckWin(item1.getX(), item1.getY());
				if (!AI.CheckEntry()) {
					EventTitle.isEqual = true;
				}
			} else {
				reset();
				AI.isLevel3 = false;
				AI.isLevel4 = false;
			}
		}

	}

	public static void CheckPlay2(int x, int y) {
		if (player_change == 2) {
			AI.chan(x, y);
			CheckWin(x, y);
			reset();
		}
		if (!AI.CheckEntry()) {
			EventTitle.isEqual = true;
		}
	}

	public static void CheckAIPlay() {
		if (!AI.CheckEntry()) {
			EventTitle.isEqual = true;
		}
		if (player_change == 2) {

			if (item1 == null) {
				AI.findPonit(10, 10, type);
			} else if (AI.isLevel3 == false && AI.isLevel4 == false) {
				AI.findPonit(item1.getX(), item1.getY(), type);
			} else {
				AI.findPonit(point.x, point.y, type);
			}
			Thread a = new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			a.start();
			AI.chan(AI.pont_defen.x, AI.pont_defen.y);
			PlayerWin = CheckWin(AI.pont_defen.x, AI.pont_defen.y);
			reset();
			onclickItem = true;
			AI.isLevel3 = false;
			AI.isLevel4 = false;
		}
	}

	public static boolean checkStep(int x, int y) {
		for (int i = 0; i < Constan.row; i++) {
			for (int j = 0; j < Constan.column; j++) {
				if (MT.list_item[x][y] == -1)
					return true;
			}

		}
		count = count - 1;
		return false;
	}

	/**
	 * check win caro 5 item not 6 item
	 * 
	 * @param row
	 * @return
	 */
	public static int checkRow(int row) {
		int dem_x = 0;
		int dem_o = 0;
		for (int i = 0; i < Constan.column; i++) {
			if (MT.list_item[row][i] == 0) {
				dem_x++;
			} else {
				dem_x = 0;
			}
			if (MT.list_item[row][i] == 1) {
				dem_o++;
			} else {
				dem_o = 0;
			}
			if (checkXO3(dem_x, dem_o)) {
				type = 1;
				point.set(row, i);
			}
			if (checkXO4(dem_x, dem_o)) {
				type = 1;
				point.set(row, i);
			}
			if (dem_x == 5) {
				// if (i == (Constan.column - 1) || MT.list_item[row][i + 1] ==
				// -1)
				return 0;
			}

			if (dem_o == 5) {
				// if (i == (Constan.column - 1) || MT.list_item[row][i + 1] ==
				// -1)
				return 1;
			}
		}
		return -1;
	}

	/**
	 * check win caro 5 item not 6 item
	 * 
	 * @param column
	 * @return
	 */
	public static int checkColumn(int column) {
		int dem_x = 0;
		int dem_o = 0;
		for (int i = 0; i < Constan.row; i++) {
			if (MT.list_item[i][column] == 0) {
				dem_x++;
			} else {
				dem_x = 0;
			}
			if (MT.list_item[i][column] == 1) {
				dem_o++;
			} else {
				dem_o = 0;
			}
			if (checkXO3(dem_x, dem_o)) {
				type = 2;
				point.set(i, column);

			}
			if (checkXO4(dem_x, dem_o)) {
				type = 2;
				point.set(i, column);

			}
			if (dem_x == 5) {
				// if (i == (Constan.row - 1) || MT.list_item[i + 1][column] ==
				// -1)
				return 0;
			}

			if (dem_o == 5) {
				// if (i == (Constan.row - 1) || MT.list_item[i + 1][column] ==
				// -1)
				return 1;
			}
		}
		return -1;
	}

	/**
	 * kiem tra duong cheo huong trai qua phai
	 * 
	 * @param row
	 * @param colum
	 * @return int
	 */
	public static int checkCross(int row, int colum) {
		int dem_x = 0;
		int dem_o = 0;
		int equal;
		int start = 0;
		int end1, end2;
		int check = 0;
		if (row < colum) {
			equal = colum - row;
			end1 = Constan.column;
			end2 = Constan.row;
			check = 1;
		} else {
			equal = row - colum;
			end1 = Constan.row;
			end2 = Constan.column;
			check = 2;
		}
		while (equal < end1 && start < end2) {
			if (check == 1) {
				if (MT.list_item[start][equal] == 0) {
					dem_x++;
				} else {
					dem_x = 0;
				}
				if (MT.list_item[start][equal] == 1) {
					dem_o++;
				} else {
					dem_o = 0;
				}
				if (checkXO3(dem_x, dem_o)) {
					type = 3;
					point.set(start, equal);

				}
				if (checkXO4(dem_x, dem_o)) {
					type = 3;
					point.set(start, equal);

				}
				if (dem_x == 5) {
					// if (equal + 1 == end1 - 1 || start + 1 == end2 - 1
					// || MT.list_item[start + 1][equal + 1] == -1)
					return 0;
				}

				if (dem_o == 5) {
					// if (equal + 1 == end1 - 1 || start + 1 == end2 - 1
					// || MT.list_item[start + 1][equal + 1] == -1)
					return 1;
				}
			} else {
				if (MT.list_item[equal][start] == 0) {
					dem_x++;
				} else {
					dem_x = 0;
				}
				if (MT.list_item[equal][start] == 1) {
					dem_o++;
				} else {
					dem_o = 0;
				}
				if (checkXO3(dem_x, dem_o)) {
					type = 3;
					point.set(equal, start);

				}
				if (checkXO4(dem_x, dem_o)) {
					type = 3;
					point.set(equal, start);

				}
				if (dem_x == 5) {
					// if (equal + 1 == end1 - 1 || start + 1 == end2 - 1
					// || MT.list_item[equal + 1][start + 1] == -1)
					return 0;
				}

				if (dem_o == 5) {
					// if (equal + 1 == end1 - 1 || start + 1 == end2 - 1
					// || MT.list_item[equal + 1][start + 1] == -1)
					return 1;
				}
			}
			equal++;
			start++;
		}
		return -1;
	}

	/**
	 * kiem tra duong cheo huong phai qua trai
	 * 
	 * @param row
	 * @param colum
	 * @return int
	 */
	public static int checkCross1(int row, int colum) {
		int dem_x = 0;
		int dem_o = 0;
		int equal;
		int start = Constan.column - 1;
		int end1;
		int check = 0;
		// test nho hon o vuong
		if (row + colum < Constan.column) {
			start = colum + row;
			end1 = Constan.column;
			equal = 0;
			check = 2;
		}// test lon hon o vuong
		else if (row + colum < Constan.row) {
			equal = row + colum - (Constan.column - 1);
			end1 = colum + row;
			start = Constan.column - 1;
			check = 2;
		} else {
			check = 1;
			end1 = Constan.column;
			equal = colum + row - (Constan.row - 1);// xac dinh equal
			start = Constan.row - 1;
		}
		while (start >= 0 && equal < end1) {

			// start < column
			if (check == 2) {
				if (MT.list_item[equal][start] == 0) {
					dem_x++;
				} else {
					dem_x = 0;
				}
				if (MT.list_item[equal][start] == 1) {
					dem_o++;
				} else {
					dem_o = 0;
				}
				if (checkXO3(dem_x, dem_o)) {
					type = 4;
					point.set(equal, start);

				}
				if (checkXO4(dem_x, dem_o)) {
					type = 4;
					point.set(equal, start);

				}

				if (dem_x == 5) {
					// if (equal + 1 == end1 - 1 || start - 1 == 0
					// || MT.list_item[equal + 1][start - 1] == -1)
					return 0;
				}

				if (dem_o == 5) {
					// if (equal + 1 == end1 - 1 || start - 1 == 0
					// || MT.list_item[equal + 1][start - 1] == -1)
					return 1;
				}
			}

			//
			else {
				if (MT.list_item[start][equal] == 0) {
					dem_x++;
				} else {
					dem_x = 0;
				}
				if (MT.list_item[start][equal] == 1) {
					dem_o++;
				} else {
					dem_o = 0;
				}
				if (checkXO3(dem_x, dem_o)) {
					type = 4;
					point.set(start, equal);

				}
				if (checkXO4(dem_x, dem_o)) {
					type = 4;
					point.set(start, equal);

				}
				if (dem_x == 5) {
					// if (equal + 1 == end1 - 1 || start - 1 == 0
					// || MT.list_item[start - 1][equal + 1] == -1)
					return 0;
				}

				if (dem_o == 5)
					// if ((equal + 1 == (end1 - 1) || start - 1 == 0)
					// || MT.list_item[start - 1][equal + 1] == -1)
					return 1;

			}
			equal++;
			start--;
		}
		return -1;
	}

	public static boolean checkXO3(int dem_x, int dem_o) {
		if (Game.isX) {
			if (dem_x == 3) {
				AI.isLevel3 = true;
				AI.isLevel4 = false;
				return true;
			}
		} else {
			if (dem_o == 3) {
				AI.isLevel3 = true;
				AI.isLevel4 = false;
				return true;
			}
		}
		return false;
	}

	public static boolean checkXO4(int dem_x, int dem_o) {
		if (Game.isX) {
			if (dem_x == 4) {
				AI.isLevel3 = false;
				AI.isLevel4 = true;
				return true;
			}
		} else {
			if (dem_o == 4) {
				AI.isLevel3 = false;
				AI.isLevel4 = true;
				return true;
			}
		}
		return false;
	}

	public static int CheckWin(int x, int y) {
		if (checkRow(x) == 0 || checkColumn(y) == 0 || checkCross(x, y) == 0
				|| checkCross1(x, y) == 0) {
			Win = true;
			return 0;
		}
		if (checkRow(x) == 1 || checkColumn(y) == 1 || checkCross(x, y) == 1
				|| checkCross1(x, y) == 1) {
			Win = true;
			return 1;
		}
		return -1;

	}
}
